package Draw0506;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;

public class DrawListenerV1 implements MouseListener, ActionListener {
    Graphics g;
    JFrame jf;

    //全局变量
    int x1,y1,x2,y2;
    String ac = "直线";

    @Override
    public void actionPerformed(ActionEvent e) {
        ac = e.getActionCommand();
        if (ac.equals("保存")) {
            Rectangle bounds = jf.getBounds();
            try {
                Robot robot = new Robot();
                BufferedImage image = robot.createScreenCapture(bounds);
                File file = new File("users.png");
                ImageIO.write(image, "png", file);
                JOptionPane.showConfirmDialog(null,"保存成功");
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    }

    @Override
    public void mouseClicked(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
    }

    @Override
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        x1 = x;
        y1 = y;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        x2 = x;
        y2 = y;
        switch (ac) {
            case "直线":
                g.drawLine(x1, y1, x2, y2);
                break;
            case "矩形":
                g.drawRect(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
                break;
            case "等腰三角形":
                g.drawLine(Math.min(x1, x2), Math.max(y1, y2), Math.max(x1, x2), Math.max(y1, y2));
                g.drawLine(Math.min(x1, x2), Math.max(y1, y2), (x1 + x2) / 2, Math.min(y1, y2));
                g.drawLine(Math.max(x1, x2), Math.max(y1, y2), (x1 + x2) / 2, Math.min(y1, y2));
                break;
            case "圆形":
                double center_x = (double) (x1 + x2) /2;
                double center_y = (double) (y1 + y2) /2;
                double round = Math.min(Math.abs(x2 - x1), Math.abs(y2 - y1)) * 0.5;
                double d;
                for (int i = y1; i <= y2; i++) {
                    for (int j = x1; j <= x2 ; j++) {
                        d = Math.sqrt(Math.pow((j - center_x),2) + Math.pow((i - center_y),2));
                        if (d >= round - 1 && d <= round + 1){  //提供误差范围，确保图形完整
                            g.fillRect(j,i,1,1);
                        }
                    }
                }
                break;
            case "椭圆":
                g.drawOval(Math.min(x1, x2), Math.min(y1, y2), Math.abs(x2 - x1), Math.abs(y2 - y1));
                break;
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
